import Base from "./base";
// 与广点通数据结构并不一致 参照query 和 back 函数
interface Targeting {
  age: Array<{ min: number | undefined, max: number | undefined }>

  geo_location: {
    location_types: Array<string>,
    regions: Array<number | string>
  },
  custom_audience: unknown,
  device_brand_model: unknown,
  device_price: unknown,
  education: unknown,
  excluded_converted_audience: unknown,
  gender: undefined | string
  marital_status: unknown,
  network_type: unknown,
  season_change_tag: unknown,
  user_os: unknown,
  wechat_ad_behavior: unknown
}
class Diect extends Base {
  targetingAccountCode: string = '';
  outTargetingId = '';
  outTargetingName = "";
  name = '';

  targeting: Targeting = {
    age: [{ min: 14, max: 66 }],
    geo_location: {
      location_types: ["LIVE_IN"],
      regions: []
    },
    custom_audience: undefined,
    device_brand_model: undefined,
    device_price: undefined,
    education: undefined,
    excluded_converted_audience: undefined,
    gender: '',
    marital_status: undefined,
    network_type: undefined,
    season_change_tag: undefined,
    user_os: undefined,
    wechat_ad_behavior: undefined
  }
  geo_location = {
    location_types: ["LIVE_IN"],
    regions: []
  }
  gender = [];

  age: Array<string> = []
  constructor(options: any) {
    super();
    this.init(options);
  }

  query() {
    const query: any = {};
    const targeting: any = this.targeting;
    Object.keys(targeting).map(key => {
      const value: any = targeting[key];
      if (key === 'gender') {
        query[key] = value ? [value] : undefined;
        return;
      }
      query[key] = value;
    });
    return query;
  }

  back(info: any) {
    info = JSON.parse(JSON.stringify(info))
    const targeting: any = this.targeting;
    Object.keys(info).map(key => {
      let value: any = info[key];
      if (key === 'gender') {
        if(value && value == 'string') {
          value = [value];
        }
        targeting[key] = Array.isArray(value) ? value[0] : '';
        return;
      }

      targeting[key] = value;
    });
    if(!targeting.gender) targeting.gender = '';
    this.targeting = targeting;
    return targeting;
  }


}

export default Diect
